//
//  KVCTest.m
//  KVCDemo
//
//  Created by shk on 14/12/9.
//  Copyright (c) 2014年 com.shk. All rights reserved.
//

#import "KVCTest.h"
#import "UserInfo.h"

@implementation KVCTest

-(void) getPublicAttribute{
    UserInfo *userInfo = [[UserInfo alloc] init];
    userInfo->email = @"songhaikang@163.com";
    NSLog(@"email:%@", userInfo->email);
}

-(void) kvcTest{
    UserInfo *userInfo = [[UserInfo alloc] init];
    [userInfo setValue:@"shk" forKey:@"username"];
    NSString *username = [userInfo valueForKey:@"username"];
    NSLog(@"username:%@", username);
}


-(void) kvcDeepTest{
    UserInfo *userInfo = [[UserInfo alloc] init];
    [userInfo setValue:@"shk" forKey:@"username"];
    NSString *username = [userInfo valueForKey:@"username"];
    NSLog(@"username:%@", username);
    
    RoleInfo *roleInfo = [[RoleInfo alloc] init];
    [roleInfo setValue:@"管理员" forKey:@"roleName"];
    [userInfo setValue:roleInfo forKey:@"roleInfo"];
    
//    [userInfo setValue:@"普通员工" forKeyPath:@"roleInfo.roleName"];
    NSString *roleName = [userInfo valueForKeyPath:@"roleInfo.roleName"];
    NSLog(@"roleName:%@", roleName);
    
    
    
    
}



-(void) kvcDeepArrayTest{
    UserInfo *userInfo = [[UserInfo alloc] init];
    NSMutableArray *relateRoles = [NSMutableArray arrayWithCapacity:3];
    for(int i=0; i<4; i++){
        RoleInfo *roleInfo = [[RoleInfo alloc] init];
        [roleInfo setValue:[NSNumber numberWithInt:i] forKey:@"roleId"];
        NSString *roleName = [NSString stringWithFormat:@"role_name_%d", i];
        [roleInfo setValue:roleName forKey:@"roleName"];
        [relateRoles addObject:roleInfo];
        
    }
    [userInfo setValue:relateRoles forKey:@"roleInfos"];
    NSArray *roleNames = [userInfo valueForKeyPath:@"roleInfos.roleName"];//取数组对象中的roleName属性的值
    NSLog(@"roleName:%@", roleNames);
    
    NSString *count = [userInfo valueForKeyPath:@"roleInfos.@count"];
    NSLog(@"count:%@", count);
    
    
    NSString *sum = [userInfo valueForKeyPath:@"roleInfos.@sum.roleId"];
    NSLog(@"sum:%@", sum);
    
    NSString *avg = [userInfo valueForKeyPath:@"roleInfos.@avg.roleId"];
    NSLog(@"avg:%@", avg);
    
    NSString *min = [userInfo valueForKeyPath:@"roleInfos.@min.roleId"];
    NSLog(@"min:%@", min);
    
    
}



@end
